package com.aooci.ordercat.aspect;

import static java.lang.Boolean.TRUE;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import com.aooci.ordercat.jsf.OrderOperationController;


@Aspect
public class PerformanceLogInterceptor implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(PerformanceLogInterceptor.class);
	
	private static final String pointcut = 
			"(" + "execution(* com.aooci.ordercat.dao.*DAO.*(..))" + 
	        " || " + "execution(* com.aooci.ordercat.service.*.*(..))" + 
			" || " + "execution(* com.aooci.ordercat.jsf.*Controller.*(..))" + ") && within(com.aooci..*)";

	@Pointcut(pointcut)
	public void pointcut() {
	}

	@Around("pointcut()")
	public Object logPerformance(ProceedingJoinPoint joinpoint) throws Throwable {
        long startTime = System.currentTimeMillis(); // get stat time

        Object obj = joinpoint.proceed();                      // method call

        long endTime = System.currentTimeMillis(); // get end time

        // record this info

        //PerformanceRecorder.record(new PerformanceUnit(joinpoint.getSignature().toShortString(), (endTime - startTime)));
        log.info("["+joinpoint.getSignature().toShortString()+"] -> " +  (endTime - startTime));
        return obj;
//		
//		PerformanceMessage msg = null;
//		LogInfo logInfo = getLogInfo(joinpoint);
//		
//		if (logInfo.getMustLog()){
//			msg = new PerformanceMessage(logInfo.getMessage());
//			msg.start();
//		}
//		try {
//			return joinpoint.proceed();
//		} finally {
//			if (msg != null) {
//				msg.stop();
//				Logger.getLogger(logInfo.getLogName()).performance(msg);
//			}
//		}
//		
	}
	

}
